/**
 * com.yhte.mir
 * Service.java
 * sunsu
 * 2012-9-18
 */
package com.yhte.mir;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.yhte.mir.center.dao.CenterDao;
import com.yhte.mir.center.vo.CenterInfo;
import com.yhte.mir.center.vo.ClientLink;
import com.yhte.mir.center.vo.IllBakInfo;
import com.yhte.mir.client.dao.ClientDao;
import com.yhte.mir.ill.dao.IllDao;
import com.yhte.mir.ill.vo.IllInfo;

/**
 * @author sunsu
 * 
 */
// @Component
public class Service {
	private static Logger log = Logger.getLogger(Service.class.getName());
	private static ApplicationContext act;
	private int thread = 0;
	private static String xml = System.getenv("YHTE_MIR") != null ? System
			.getenv("YHTE_MIR") : "../conf/beans*.xml";

	static {
		log.info("===== " + xml + " : " + System.getenv("YHTE_MIR"));
		try {
			act = new FileSystemXmlApplicationContext(xml);
		} catch (Exception e) {
			log.info("====static " + e.getMessage());
		}
	}

	// @Scheduled(cron="0 * * * * ?")
	public void service() {
		try {
			if (act == null) {
				act = new FileSystemXmlApplicationContext(xml);
			}
			thread = 0;
			IllDao illDao = (IllDao) act.getBean("illDao");
			CenterDao center = (CenterDao) act.getBean("centerDao");
			ClientDao client = (ClientDao) act.getBean("clientDao");

			// test
			List<IllInfo> tempills = illDao.findAll();
			center.insertHmd(tempills);
			// end

			// 查询查报站datalink
			List<ClientLink> clientLinks = center.findDataLink();
			log.info("dataLink：" + clientLinks.size());

			for (ClientLink link : clientLinks) {
				SynchClass sc = new SynchClass(client, center,
						link.getDataLink());
				sc.start();
			}

		} catch (Exception e) {
			log.info("====Exception " + e.getMessage());
			e.printStackTrace();
		}
	}

}

/**
 * @author Administrator
 *同步多次违法车辆
 */
class SynchClass extends Thread {
	private static Logger log = Logger.getLogger(SynchClass.class.getName());
	private ClientDao client;
	private CenterDao center;
	private String link;

	public SynchClass(ClientDao client, CenterDao center, String link) {
		this.client = client;
		this.center = center;
		this.link = link;
	}

	public void run() {

		// 查询未同步的违法车辆
		List<IllInfo> ills = center.findAllHmd(link);
		
		log.info(this.getName() + " : " + link + " 同步违法车辆开始，共"+ills.size()+"辆");
		
		if(ills != null && !ills.isEmpty()){
			client.insert(link,ills);
		}
		new DupCenterHphm(center,link).start();
		log.info(this.getName() + " : " + link + " 同步违法车辆结束");
	}
}

/**
 * @author Administrator
 * 删除前端在交管库中没有的多次违法车辆
 */
class DupCenterHphm extends Thread {
	private static Logger log = Logger.getLogger(SynchClass.class.getName());
	private CenterDao center;
	private String link;

	public DupCenterHphm(CenterDao center,String link) {
		this.center = center;
		this.link = link;
	}

	public void run() {
		log.info("删除前端在交管库中没有的多次违法车辆开始");
		List<CenterInfo> dupHphms = center.findDupHphm(link);
		log.info("在车管库中不存在的违法车辆有"+dupHphms.size()+"辆");
		if(dupHphms != null && !dupHphms.isEmpty()){
			center.delDupHphm(link, dupHphms);
		}
		log.info("删除前端在交管库中没有的多次违法车辆结束");
	}
}
